package sc.love.boot.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailTools {

    @Autowired
    private JavaMailSender javaMailSender;

    public String send(String topic,String text,String from ,String to){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to.split(","));
        message.setSubject(topic);
        message.setText(text);

        try {
            javaMailSender.send(message);
            System.out.println("郵件發送成功");
            return "郵件發送成功";
        } catch (MailException e) {
            e.printStackTrace();
            System.out.println("郵件發送失敗");
        }
        return "郵件發送失敗";
    }
}
